﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SpaceSquirrel.SpecialLevelStuff
{
	public class VolcanoCoreActivation : SpecialLevel
	{
		private int id;
		private int spotX;
		private int spotY;
		private int sequenceCounter;
		private bool done;
		private bool warpBack;

		public VolcanoCoreActivation(PlayScene scene, int id)
			: base(scene)
		{
			this.id = id;
			this.scene = scene; // redundant?
			this.context = scene.Context;
			this.HasUpdate = true;
			this.HasPostInit = true;
			this.spotX = 11;
			this.spotY = 9;
			this.sequenceCounter = -1;
			this.done = false;
			this.lavaLevel = 0;
			this.shakeScreen = false;
			this.freeze = false;
			this.warpBack = false;
		}

		public override void PostInit()
		{
			bool isLavaOn = false;
			if (this.id == 1)
			{
				isLavaOn = this.context.VolcanoA;
			}
			else if (this.id == 2)
			{
				isLavaOn = this.context.VolcanoB;
			}
			else if (this.id == 3)
			{
				isLavaOn = this.context.VolcanoC;
			}

			if (isLavaOn)
			{
				this.DoLavaSequence(true);
			}
		}

		public void DoLavaSequence(bool instant)
		{
			this.done = true;
			if (instant)
			{
				this.sequenceCounter = 9999999;
			}
			else
			{
				this.sequenceCounter = 0;
			}

			bool complete = this.context.VolcanoA && this.context.VolcanoB && this.context.VolcanoC;

			if (!complete)
			{
				if (this.id == 1)
				{
					this.context.VolcanoA = true;
				}
				else if (this.id == 2)
				{
					this.context.VolcanoB = true;
				}
				else if (this.id == 3)
				{
					this.context.VolcanoC = true;
				}

				complete = this.context.VolcanoA && this.context.VolcanoB && this.context.VolcanoC;

				if (complete)
				{
					this.warpBack = true;
				}
			}
		}

		public override void Update()
		{
			Sprite player = this.scene.Player;
			int tx = player.x / 16;
			int ty = player.y / 16;

			if (tx == this.spotX && Math.Abs(ty - this.spotY) < 2 && !this.done)
			{
				this.DoLavaSequence(false);
			}

			this.freeze = false;
			this.shakeScreen = false;
			this.scene.Player.sprinkle = false;

			if (this.sequenceCounter >= 0)
			{
				int SPRINKLE = 30; // player sprinkles mix
				int SHAKE = 60; // screen shakes
				int RISE_RATE = 30; // this happens 3 times

				this.freeze = true;
				int sc = this.sequenceCounter;
				if (sc < SPRINKLE)
				{
					this.scene.Player.sprinkle = true;
					if (sc == 0)
					{
						SoundManager.PlayNoise("sprinkle_lava_packet");
					}
					double progress = 1.0 * sc / SPRINKLE;
				}
				else
				{
					sc -= SPRINKLE;
					if (sc < SHAKE)
					{
						if (sc == 0)
						{
							SoundManager.PlayNoise("screen_shaking");
						}
						this.shakeScreen = (sc & 1) == 0;
					}
					else
					{
						sc -= SHAKE;
						if (sc < RISE_RATE)
						{
							if (sc == 0)
							{
								SoundManager.PlayNoise("lava_rise");
							}
							this.lavaLevel = 1;
						}
						else
						{
							sc -= RISE_RATE;
							if (sc < RISE_RATE)
							{
								if (sc == 0)
								{
									SoundManager.PlayNoise("lava_rise");
								}
								this.lavaLevel = 2;
							}
							else
							{
								sc -= RISE_RATE;
								this.lavaLevel = 3;
								if (sc < RISE_RATE)
								{
									if (sc == 0)
									{
										SoundManager.PlayNoise("lava_rise");
									}
								}
								else
								{
									this.freeze = false;
									if (this.warpBack)
									{
										this.scene.Next = new VolcanoCompleteScene(this.scene, this.context);
									}
								}
							}
						}
					}
				}

				this.sequenceCounter++;
			}
		}
	}
}
